#!/usr/bin/env python
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'

__license__ = 'GPL v3'

'''
calibre recipe for slate.com
'''

from calibre.web.feeds.recipes import BasicNewsRecipe


def classes(classes):
    q = frozenset(classes.split(' '))
    return dict(attrs={
        'class': lambda x: x and frozenset(x.split()).intersection(q)})


class Slate(BasicNewsRecipe):
    title = 'Slate'
    description = 'A general-interest publication offering analysis and commentary about politics, news and culture.'
    __author__ = 'Kovid Goyal'
    timefmt = ''
    no_stylesheets = True
    language = 'en'
    encoding = 'utf-8'
    remove_attributes = ['style']
    INDEX = 'https://slate.com'
    compress_news_images = True

    keep_only_tags = [
        classes('article__header article__content'),
    ]
    remove_tags = [
        dict(name='ul', attrs={'class':"social-share"}),
    ]

    def preprocess_html(self, soup):
        for img in soup.findAll('img', attrs={'data-srcset': True}):
            img['src'] = img['data-srcset'].split()[0]
        return soup

    def parse_index(self):
        ans = []
        for sectitle, url in (
                ('News & Politics', '/articles/news_and_politics.html'),
                ('Technology', '/articles/technology.html'),
                ('Business', '/articles/business.html'),
                ('Arts', '/articles/arts.html'),
                ('Life', '/articles/life.html'),
                ('Health & Science', '/articles/health_and_science.html'),
                ('Sports', '/articles/sports.html'),
                ('Double X', '/articles/double_x.html'),
        ):
            url = self.INDEX + url
            self.log('\nFound section:', sectitle)
            articles = self.slate_section_articles(self.index_to_soup(url))
            if articles:
                ans.append((sectitle, articles))
            if self.test and len(ans) > 1:
                break
        return ans

    def slate_section_articles(self, soup):
        ans = []
        main = soup.find('article', attrs={'class': 'main'})
        if main is None:
            return ans
        for a in main.findAll('a', attrs={'class': 'primary'}):
            url = a['href']
            if url.endswith('/'):
                continue
            p = a.parent
            title = p.find(attrs={'class': 'hed'})
            if title is None:
                continue
            title = self.tag_to_string(title)
            span = p.find(attrs={'class': 'byline'})
            desc = ''
            if span is not None:
                desc = self.tag_to_string(span)
            self.log('\t' + title)
            self.log('\t\t' + url)
            ans.append({'title': title, 'description': desc,
                        'date': '', 'url': url})
        return ans
